//
//  StrategyViewController.m
//  Travel
//
//  Created by lanou on 15/9/18.
//  Copyright (c) 2015年 TeamThree. All rights reserved.
//

#import "StrategyViewController.h"
#import "TitleScrollView.h"
#import "TableScrollView.h"
#import "StrategyTableViewCell.h"
#define kAutoWidth 375 * kWidth
#define kAutoHight 667 * kHeight

@interface StrategyViewController ()<UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) TitleScrollView *titleScrollView;
@property (nonatomic, retain) TableScrollView *tableScrollView;
@end

@implementation StrategyViewController

- (void)dealloc
{
    [_titleScrollView release];
    [_tableScrollView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;    
    self.view.backgroundColor = [UIColor colorWithRed:0.969 green:0.949 blue:0.902 alpha:1.000];
    
    //添加页面上方一排按钮，可以滚动
    [self addTitleScrollView];
    
    //添加滑动页面
    [self addTableScrollView];
    
    
}


#pragma mark ------设置滚动的页面------

- (void)addTableScrollView {
    _tableScrollView = [[TableScrollView alloc] initWithFrame:CGRectMake(0, 64 + 30, kWidth, kHeight - 64 - 30) contentArr:nil];
    
    _tableScrollView.delegate = self;

    for (int i; i < 7 ; i++) {
        UITableView *tableView = (UITableView *)[_tableScrollView viewWithTag:i + 2000];
        tableView.separatorStyle = NO;
        tableView.backgroundColor = [UIColor colorWithRed:0.969 green:0.949 blue:0.902 alpha:1.000];
        tableView.contentInset = UIEdgeInsetsMake(0, 0, 59, 0);
        [tableView registerClass:[StrategyTableViewCell class] forCellReuseIdentifier:@"myCell"];
        tableView.delegate = self;
        tableView.dataSource = self;
    }

    [self.view addSubview:_tableScrollView];
}

#pragma mark ------tableView代理方法------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StrategyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120.0 / kAutoHight;
}

#pragma mark ------滚动改变选中的btn------
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSLog(@"滑动");
    NSInteger tag = _tableScrollView.contentOffset.x / kWidth + 10000;
    UIButton *btn = (UIButton *)[_titleScrollView viewWithTag:tag];
    
    [self.titleScrollView setButtonToDefault];
    [UIView animateWithDuration:2 animations:^{
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }];
}

#pragma mark ------设置页面上方一排按钮，可以滚动------

- (void)addTitleScrollView {
    
    //标题文字
    NSArray *titleArr = @[@"最新",@"热门",@"亚洲",@"欧洲",@"美洲",@"非洲",@" 大洋洲"];
    
    self.titleScrollView = [[TitleScrollView alloc] initWithFrame:CGRectMake(0, 64, kWidth, 30) titleArr:titleArr target:self action:@selector(clickBtnAction:) controllEvent:UIControlEventTouchUpInside];
    [self.view addSubview:self.titleScrollView];
    [_titleScrollView release];
}


#pragma mark ------上面的一排标题的点击事件------

- (void)clickBtnAction:(UIButton *)btn {
    
    [self.titleScrollView setButtonToDefault];
    [UIView animateWithDuration:2 animations:^{
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }];
    
    [_tableScrollView setContentOffset:CGPointMake((btn.tag - 10000) * kWidth, 0) animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
